/*
 * DesktopTag.java
 *
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the flyingsoft, Version 1.0 (the "License");
 *
 * Company:北京东方飞扬软件股份有限公司
 * 主页：http://www.flyingsoft.cn
 *
 */
package org.geek.app.login.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import org.geek.core.bean.ExtTree;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * <p>用于生成桌面的Tag标签</p>
 * @author ninglong 2011-12-21下午04:57:52
 * @version 1.0
 */
public class DesktopTag extends TagSupport {
	
	private static final long serialVersionUID = -7788593927281132473L;
	private ApplicationContext applicationContext;
	private long time = System.currentTimeMillis();
	 
	private List<ExtTree> list = new ArrayList<ExtTree>(); 
	
	public ApplicationContext getApplicationContext() {
		return applicationContext;
	  }

    public int doStartTag()throws JspException{
    	applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(this.pageContext.getServletContext());
    	StringBuffer buffer = new StringBuffer();
    	genernateHtml(buffer);
    	buffer.append("<script type='text/javascript'>																").append("\r\n");
    	genernateJavaScript(buffer);
    	buffer.append("</script>																					").append("\r\n");
    	write(this.pageContext, buffer);
    	return EVAL_BODY_INCLUDE;
   }

	public int doEndTag() throws JspException{
		return EVAL_PAGE;
	}
	
	public void release(){
	    super.release();
	    this.applicationContext = null;
	    this.list = null;
	}
	  

	private void write(PageContext pageContext, StringBuffer buffer)throws JspException{
	    JspWriter out = pageContext.getOut();
	    try {
	      out.write(buffer.toString());
	    } catch (IOException e) {
	      throw new JspException(e);
	    }
	}
	
	protected void genernateJavaScript(StringBuffer buffer){
		buffer.append("Ext.namespace('Ext.ux','geek');													").append("\r\n");
		buffer.append("geek.app = new Ext.app.App({																").append("\r\n");
	    buffer.append("		init :function(){															").append("\r\n");
	    buffer.append("			Ext.QuickTips.init();													").append("\r\n");
	    buffer.append(" 	},																			").append("\r\n");
	   
	    buffer.append(" 	modules:[																	").append("\r\n");
	    
	    //开始菜单配置
	    buildStartItem(list,buffer);
		
	    buffer.append(" 	],																			").append("\r\n");
		buffer.append(" 	desktopConfig: {															").append("\r\n");
		buffer.append(" 	appearance: {},																").append("\r\n");
		//桌面背景配置
		buffer.append(" 	background: {},																").append("\r\n");
		buffer.append(" 	launchers: {																").append("\r\n");
		//自动运行配置
		buffer.append(" 		'autorun': [],															").append("\r\n");
	    //快速启动栏配置
		buffer.append(" 		'quickstart': [															").append("\r\n");
	    for (int i = 0; i < list.size(); i++) {
	      ExtTree menu = list.get(i);
	      if (i == list.size() - 1)
	        buffer.append(" '" + menu.getName() +time+ "'												").append("\r\n");
	      else {
	        buffer.append(" '" + menu.getName() +time+ "',												").append("\r\n");
	      }
	    }
	    buffer.append(" 		],																		").append("\r\n");
	    //桌面图标配置
	    buffer.append(" 		'shortcut': [															").append("\r\n");
	    for (int i = 0; i < list.size(); i++) {
	    	 ExtTree menu = list.get(i);
	      if (i == list.size() - 1)
	        buffer.append(" '" + menu.getName() +time+ "'												").append("\r\n");
	      else {
	        buffer.append(" '" + menu.getName() +time+ "',												").append("\r\n");
	      }
	    }
	    buffer.append(" 		]																		").append("\r\n");
	    buffer.append(" 	},																			").append("\r\n");
	    //开始菜单面板配置
	    buffer.append(" 	taskbarConfig: {															").append("\r\n");
	    buffer.append(" 		buttonScale: 'large',													").append("\r\n");
	    buffer.append(" 		position: 'bottom',														").append("\r\n");
	    buffer.append(" 		quickstartConfig: {														").append("\r\n");
	    buffer.append(" 			width: 120															").append("\r\n");
	    buffer.append(" 		},																		").append("\r\n");
	    buffer.append(" 		startButtonConfig: {													").append("\r\n");
	    buffer.append(" 			iconCls: 'icon-qwikioffice',										").append("\r\n");
	    buffer.append(" 			text: '开始'															").append("\r\n");
	    buffer.append(" 		},																		").append("\r\n");
	    buffer.append(" 		startMenuConfig: {														").append("\r\n");
	    buffer.append(" 			iconCls: 'icon-user-48',											").append("\r\n");
	    buffer.append(" 			title:'admin',    													").append("\r\n");
	    buffer.append(" 			width: 300															").append("\r\n");
	    buffer.append(" 		}																		").append("\r\n");
	    buffer.append(" 	}																			").append("\r\n");
	    buffer.append("  }																				").append("\r\n");
	    buffer.append("});																				").append("\r\n");
	  }
	
	protected void genernateHtml(StringBuffer buffer){
	
		buffer.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"resources/css/shortcut.css\" />								").append("\r\n");
	    
		buffer.append("<script type=\"text/javascript\" src=\"resources/js/ux/desktop/Module.js\"></script>").append("\r\n");
		buffer.append("<script type=\"text/javascript\" src=\"resources/js/ux/desktop/App.js\"></script>").append("\r\n");
		buffer.append("<script type=\"text/javascript\" src=\"resources/js/ux/desktop/TaskBar.js\"></script>").append("\r\n");
		buffer.append("<script type=\"text/javascript\" src=\"resources/js/ux/desktop/Shortcut.js\"></script>").append("\r\n");
		buffer.append("<script type=\"text/javascript\" src=\"resources/js/ux/desktop/Notification.js\"></script>").append("\r\n");
		buffer.append("<script type=\"text/javascript\" src=\"resources/js/ux/desktop/StartMenu.js\"></script>").append("\r\n");
		buffer.append("<script type=\"text/javascript\" src=\"resources/js/ux/desktop/Desktop.js\"></script>").append("\r\n");
		
	}
	
	/**
	 * 开始菜单配置
	 * @param chrild
	 * @param buffer
	 */
	private void buildStartItem(List<ExtTree> chrild,StringBuffer buffer){
	    for (int i = 0; i < chrild.size(); i++) {
		      ExtTree menu = chrild.get(i);
		      buffer.append("    {										").append("\r\n");
		      buffer.append("    	'id':'" + menu.getName() +time+ "',										").append("\r\n");
		      buffer.append("    	'url':'" + menu.getUrl() + "',									").append("\r\n");
		      buffer.append("    	'className':'geek." + menu.getName() +time+ "',							").append("\r\n");
		      buffer.append("    	'launcher':{															").append("\r\n");
		      buffer.append("    		'iconCls':'icon-" + menu.getName() + "',						").append("\r\n");
		      buffer.append("    		'shortcutIconCls':'" + menu.getName() + "-shortcut',			").append("\r\n");
		      buffer.append("    		'width':800,								").append("\r\n");
		      buffer.append("    		'height':600,							").append("\r\n");
		      buffer.append("    		'parentName':'"+menu.getPatentName()+"',							").append("\r\n");
		      buffer.append("    		'text':'" + menu.getText() + "',								").append("\r\n");
		      buffer.append("    		'tooltip':'" + menu.getText() + "'								").append("\r\n");
		      buffer.append("    	 },																		").append("\r\n");
		      buffer.append("       'launcherPaths':{'startmenu':'"+menu.getParentPath()+"'}						").append("\r\n");
		      if (i == chrild.size() - 1){
		    	  buffer.append("}").append("\r\n"); 
		      }else{
		    	  buffer.append("},").append("\r\n"); 
		      }
		    }
	}
	public List<ExtTree> getList() {
		return list;
	}

	public void setList(List<ExtTree> list) {
		this.list = list;
	}
}
